﻿using BCCommon;
using BCEntity.TMS.DisableArea;
using Dapper;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace BCData.TMS.DisableArea
{
    public class DisableAreaData : IDisableAreaData
    {
        private readonly IDatabaseContext databaseContext;

        private const string QUERY_DISABLEAREA =
            @"SELECT DisableAreaId,ProvinceCode,CityCode,CountyCode,RangeCoordinate,AdminId,DisableType,OpenTime,CloseTime,Enabled,CreateTime,UpdateTime,Editer,EditerAdminId 
            FROM disablearea";

        private const string INSERT_DISABLEAREA =
            @"INSERT INTO disablearea (ProvinceCode,CityCode,CountyCode,RangeCoordinate,AdminId,DisableType,OpenTime,CloseTime,Enabled,CreateTime,UpdateTime,Editer,EditerAdminId)
            VALUES(?ProvinceCode,?CityCode,?RangeCoordinate,?AdminId,?DisableType,?OpenTime,?CloseTime,?Enabled,NOW(),NOW(),?Editer,?EditerAdminId)";

        private const string DELETE_DISABLEAREA =
            @"DELETE FROM disablearea WHERE DisableAreaId=?DisableAreaId";

        private const string DELETE_DISABLEAREARANGE =
            @"DELETE FROM DisableAreaRangeCoordinate WHERE DisableAreaId=?DisableAreaId";

        public DisableAreaData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        /// <summary>
        /// 获取禁卸货区域
        /// </summary>
        /// <returns></returns>
        public IEnumerable<DisableAreaEntity> GetDisableAreaInfo(string provinceCode, string cityCode)
        {
            DynamicParameters parameters = new DynamicParameters();
            List<string> wheres = new List<string>();
            if (!string.IsNullOrEmpty(provinceCode))
            {
                wheres.Add("ProvinceCode = ?ProvinceCode");
                parameters.Add("?ProvinceCode", provinceCode, DbType.AnsiString);
            }
            if (!string.IsNullOrEmpty(cityCode))
            {
                wheres.Add("CityCode = ?CityCode");
                parameters.Add("?CityCode", cityCode, DbType.AnsiString);
            }

            string sql = string.Format(@"{0} {1}", QUERY_DISABLEAREA,
                 wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "");

            return this.databaseContext.Master.Query<DisableAreaEntity>(sql, parameters);
        }

        /// <summary>
        /// 新增禁卸货区域信息
        /// </summary>
        /// <returns></returns>
        public async Task<DisableAreaEntity> AddDisableArea(DisableAreaEntity entity)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = string.Format(@"{0}; {1} WHERE DisableAreaId=LAST_INSERT_ID();", INSERT_DISABLEAREA, QUERY_DISABLEAREA);
            parameters.Add("?ProvinceCode", entity.ProvinceCode, DbType.AnsiString, size: 20);
            parameters.Add("?CityCode", entity.CityCode, DbType.AnsiString, size: 20);
            parameters.Add("?RangeCoordinate", entity.RangeCoordinate, DbType.AnsiString, size: 4000);
            parameters.Add("?AdminId", entity.AdminId, DbType.Int64, size: 20);
            parameters.Add("?DisableType", entity.DisableType.ToString(), DbType.AnsiString);
            parameters.Add("?OpenTime", entity.OpenTime, DbType.DateTime);
            parameters.Add("?CloseTime", entity.CloseTime, DbType.DateTime);
            parameters.Add("?Enabled", entity.Enabled, DbType.Boolean);
            parameters.Add("?Editer", entity.Editer, DbType.AnsiString, size: 30);
            parameters.Add("?EditerAdminId", entity.EditerAdminId, DbType.Int64, size: 20);

            var result = await this.databaseContext.Master.QueryFirstOrDefaultAsync<DisableAreaEntity>(sql, parameters);
            return result;
        }

        /// <summary>
        /// 删除禁卸货区域信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(long disableAreaId)
        {
            DynamicParameters parameters = new DynamicParameters();

            parameters.Add("?DisableAreaId", disableAreaId, DbType.Int64, size: 20);
            object result = this.databaseContext.Master.ExecuteScalar(DELETE_DISABLEAREA, parameters);
            return result != null;
        }

        public async Task<DisableAreaEntity> UpdateDisableArea(DisableAreaEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("disablearea", new string[] { "RangeCoordinate", "DisableType", "OpenTime", "CloseTime", "Editer", "EditerAdminId" }
                , new string[] { "DisableAreaId" });
            sql = string.Format("{0} {1} WHERE DisableAreaId=?DisableAreaId", sql, QUERY_DISABLEAREA);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?RangeCoordinate", entity.RangeCoordinate, DbType.AnsiString, size: 4000);
            parameters.Add("?DisableType", entity.DisableType.ToString(), DbType.AnsiString);
            parameters.Add("?OpenTime", entity.OpenTime, DbType.DateTime);
            parameters.Add("?CloseTime", entity.CloseTime, DbType.DateTime);
            parameters.Add("?Editer", entity.Editer, DbType.AnsiString, size: 30);
            parameters.Add("?EditerAdminId", entity.EditerAdminId, DbType.Int64, size: 20);
            parameters.Add("?DisableAreaId", entity.DisableAreaId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<DisableAreaEntity>(sql, parameters);

        }
    }
}
